package com.stray.crawl.exec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.stray.crawl.BeanContext;
import com.stray.crawl.model.dao.PlanRepository;

public final class Main {

	protected static final Log log = LogFactory.getLog(Main.class);

	// DONE
	// remove use as referer, use referer is enough
	// add skip last row (skipFirst, skipLast)
	// Plan group (eg. hkg, itpub)
	// unused field to be ignore
	// forum have thumbnail
	// group PageResultContext into ResultContext, align with TaskContext
	// working on *** expression > to full formatted expression
	// standardize forum page, eg hkg param message > threadId, forumId, page
	// elements > .ownText()
	// enhance trim with charact other than space, eg [] ()
	// enhance substr with after / before
	// add remove element parser
	// Setup test case ... build hudson ! continue integration

	// TODO items list
	// Param with POST request
	// Param pange > regualr expression ?
	// cancel long running / waiting task
	// extra information, following url, images / resource url, page number, rating
	// handle fourm content - with attachement and qoute
	// handle fourm content - header icon
	// storage to result entity
	// CrawlTask to be comparable

	// parsers
	// enhance date, make with auto format without pattern

	public static void main(String[] args) {
		BeanContext ctx = null;

		try {
			ctx = BeanContext.getContext();
			PlanRepository repo = BeanContext.get(PlanRepository.class);

//			repo.exportPlans("storage\\plans");
			repo.buildSchema();
			repo.importPlans("storage\\plans");

			/*
			service.addListener(new EventListener<CrawlTask, TaskStatus>() {

				@Override
				public void onEvent(CrawlTask source, TaskStatus status) {
					log.info("> Event: " + status + " of " + source);
					service.printStatus();
				}

			});

			Map<String, String> map = new HashMap<String, String>();
			map.put(HttpHeaderKey.REFERER, "http://forum2.hkgolden.com/topics_bw.htm");

			CrawlTask task = service.submit("http://forum2.hkgolden.com/view.aspx?message=3913107&type=BW", map);

			System.in.read();

			ResultContext rctx = service.obtainResult(task);

			java.util.List<Object[]> results = rctx.getResults();
			for (Object[] objs : results) {
				for (Object obj : objs) {
					System.out.print(obj + "\t\t");
				}
				System.out.println();
			}

			// ---------------------------------------------------------------------
			java.util.List<String> imageUrls = rctx.getImageUrls();
			java.util.Set<String> urls = new java.util.HashSet<>(imageUrls);
			HashMap<String, String> configs = new HashMap<>();
			configs.put(HttpHeaderKey.REFERER, "http://forum4.hkgolden.com/view.aspx?message=3892336&type=BW");

			for (String url : urls) {
				System.out.println(url);

				service.submit(url, configs);
			}

			while (System.in.read() != 'Q') {
				System.out.println("---------------------------------------------------------------------");
				service.printStatus();
				System.out.println("---------------------------------------------------------------------");
			}*/
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (ctx != null) {
					ctx.close();
				}
			} catch (Exception e) {
				// ignore
			}
		}
	}

}